<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use App\Rules\VerificationCodeRule;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class MailController extends Controller
{
    public function create()
    {
        return view('member.email');
    }

    public function store(Request $request)
    {

        $user = Auth::user();
        $request->validate
        (
            ['email' => ['email', Rule::unique('users', 'email')->ignore($user->id)],
                'code' => [new VerificationCodeRule($request->email), 'required']
            ], ['code.required' => '验证码不能为空']
        );

        $user->email = $request->email;
        $user->save();
        return response()->json(['message' => '邮箱修改成功']);
    }
}
